package com.myzl.coal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myzl.coal.domain.DeviceGroup;
import com.myzl.coal.domain.vo.DeviceGroupVO;
import com.myzl.coal.mapper.DeviceGroupMapper;
import com.myzl.coal.service.IDeviceGroupService;
import com.myzl.common.constant.UserConstants;
import com.myzl.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备组Service业务层处理
 *
 * @author myzl
 * @date 2022-11-22
 */
@Service
public class DeviceGroupServiceImpl extends ServiceImpl<DeviceGroupMapper, DeviceGroup> implements IDeviceGroupService
{
    @Autowired
    private DeviceGroupMapper deviceGroupMapper;

    /**
     * 查询设备组
     *
     * @param id 设备组主键
     * @return 设备组
     */
    @Override
    public DeviceGroup selectDeviceGroupById(Long id)
    {
        return deviceGroupMapper.selectDeviceGroupById(id);
    }

    /**
     * 查询设备组列表
     *
     * @param deviceGroup 设备组
     * @return 设备组
     */
    @Override
    public List<DeviceGroup> selectDeviceGroupList(DeviceGroup deviceGroup)
    {
        return deviceGroupMapper.selectDeviceGroupList(deviceGroup);
    }

    /**
     * 新增设备组
     *
     * @param deviceGroup 设备组
     * @return 结果
     */
    @Override
    public int insertDeviceGroup(DeviceGroup deviceGroup)
    {
        deviceGroup.setCreateTime(DateUtils.getNowDate());
        return deviceGroupMapper.insertDeviceGroup(deviceGroup);
    }

    /**
     * 修改设备组
     *
     * @param deviceGroup 设备组
     * @return 结果
     */
    @Override
    public int updateDeviceGroup(DeviceGroup deviceGroup)
    {
        deviceGroup.setUpdateTime(DateUtils.getNowDate());
        return deviceGroupMapper.updateDeviceGroup(deviceGroup);
    }

    /**
     * 批量删除设备组
     *
     * @param ids 需要删除的设备组主键
     * @return 结果
     */
    @Override
    public int deleteDeviceGroupByIds(Long[] ids)
    {
        return deviceGroupMapper.deleteDeviceGroupByIds(ids);
    }

    /**
     * 删除设备组信息
     *
     * @param id 设备组主键
     * @return 结果
     */
    @Override
    public int deleteDeviceGroupById(Long id)
    {
        return deviceGroupMapper.deleteDeviceGroupById(id);
    }

    @Override
    public List<DeviceGroupVO> selectDeviceGroupByType(Integer groupType) {
        List<DeviceGroupVO> result = new ArrayList<>();
        List<DeviceGroup> deviceGroups = deviceGroupMapper.selectList(new LambdaQueryWrapper<DeviceGroup>()
                .eq(DeviceGroup::getGroupType, groupType)
                .eq(DeviceGroup::getParentId, UserConstants.PARENT_ID));
        for (DeviceGroup deviceGroup : deviceGroups) {
            DeviceGroupVO deviceGroupVO = new DeviceGroupVO();
            deviceGroupVO.setId(deviceGroup.getId());
            deviceGroupVO.setValue(deviceGroup.getGroupName());
            result.add(deviceGroupVO);
        }
        return result;
    }

    @Override
    public List<DeviceGroupVO> selectDeviceGroupByParentId(Integer parentId) {
        List<DeviceGroupVO> result = new ArrayList<>();
        List<DeviceGroup> deviceGroups = deviceGroupMapper.selectList(new LambdaQueryWrapper<DeviceGroup>()
                .eq(DeviceGroup::getParentId, parentId));
        for (DeviceGroup deviceGroup : deviceGroups) {
            DeviceGroupVO deviceGroupVO = new DeviceGroupVO();
            deviceGroupVO.setId(deviceGroup.getId());
            deviceGroupVO.setValue(deviceGroup.getGroupName());
            result.add(deviceGroupVO);
        }
        return result;
    }
}
